<?php
namespace App\Admin\Controllers\Basics;

use App\Models\Basics\SysCountry;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
class SysCountryController extends AdminController{

    protected $title = '国家设置';

    protected function grid()
    {
        $grid = new Grid(new SysCountry());
        $grid->column('id','ID')->style('width: 5%');
        $grid->column('country','编码');
        $grid->column('country_cn','名称');
        $grid->column('desc','描述');

        $grid->actions(function (Grid\Displayers\Actions $actions) {
            $actions->disableView();
        });

        $grid->quickCreate(function (Grid\Tools\QuickCreate $create) {
            $create->text('country', '编码');
            $create->text('country_cn', '名称');
            $create->text('desc', '描述');
        });
        $grid->disableRowSelector();
        $grid->disableExport();
        $grid->disableFilter();
        $grid->quickSearch('country', 'country_cn');

        return $grid;
    }

    public function form()
    {
        $form = new Form(new SysCountry());

        $form->hidden('id');

        $form->text('country', '编码')
            ->creationRules(['required', "unique:b_sys_country"])
            ->updateRules(['required', "unique:b_sys_country,country,{{id}}"]);
        $form->text('country_cn', '名称')->required()->rules('required',['required' => '名称不能为空']);
        $form->text('desc', '描述');
        $form->saving(function (Form $form) {
            if ($form->country && $form->model()->country != $form->country) {
                $form->country = strtoupper($form->country);
            }
        });
        $form->tools(function (Form\Tools $tools) {
            $tools->disableView();
        });
        return $form;
    }
}
